#!/bin/sh
# shellcheck shell=dash disable=SC2169,SC3010 source=/dev/null
#
# Starts ReGaHss.
#

CREATE_DIRS='/usr/local/etc/config/rc.d /usr/local/etc/config/addons/www'

LOGLEVEL_REGA=2
REGA_BIN_VERSION=
PIDFILE=/var/run/ReGaHss.pid

[[ -r /var/hm_mode ]] && . /var/hm_mode

# skip this startup if not in normal mode
[[ "${HM_MODE}" != "NORMAL" ]] && exit 0

if [[ -s /etc/config/ReGaHssVersion ]]; then
  REGA_BIN_VERSION=$(cat /etc/config/ReGaHssVersion)
fi
case "${REGA_BIN_VERSION}" in
  NORMAL)    REGA_BIN_FILE="ReGaHss.normal";;
  COMMUNITY) REGA_BIN_FILE="ReGaHss.community";;
  DEBUG)     REGA_BIN_FILE="ReGaHss.debug";;
  *)         REGA_BIN_FILE="ReGaHss";;
esac
if [[ ! -f "/bin/${REGA_BIN_FILE}" ]]; then
  REGA_BIN_FILE="ReGaHss"
fi

init() {
  for i in ${CREATE_DIRS}; do
    [[ ! -d ${i} ]] && mkdir -p "${i}"
  done
       
  if [[ ! -d /etc/config/userprofiles ]]; then
    mkdir -p /etc/config/userprofiles
  fi

  if [[ -f /etc/config/syslog ]]; then
    . /etc/config/syslog
  fi

  # source profile.d files
  if [[ ! -e /etc/config/safemode ]] ; then
    # source from /etc/profile.d
    for i in /etc/profile.d/*.sh ; do
      if [[ -r "$i" ]]; then
        . "$i"
      fi
      unset i
    done

    # source from /usr/local/etc/profile.d
    if [[ -d /usr/local/etc/profile.d ]]; then
      for i in /usr/local/etc/profile.d/*.sh ; do
        if [[ -r "$i" ]]; then
          . "$i"
        fi
        unset i
      done
    fi
  fi
}

waitStartupComplete() {
  STEPS=20
  for i in $(seq 1 ${STEPS}); do
    sleep 2
    echo -n "."
    if [[ -n "$(pidof ${REGA_BIN_FILE})" ]]; then
      echo "OK"
      break
    fi
    if [[ ${i} -eq ${STEPS} ]]; then
      echo "ERROR"
    fi
  done
}

waitTerminationComplete() {
  STEPS=20
  for i in $(seq 1 ${STEPS}); do
    sleep 2
    echo -n "."
    if [[ -z "$(pidof ${REGA_BIN_FILE})" ]]; then
      echo "OK"
      break
    fi
    if [[ ${i} -eq ${STEPS} ]]; then
      echo "ERROR"
    fi
  done
}

start() {
  echo -n "Starting ReGaHss: "
  init

  # make sure ReGaHss will run on umask 0027 per default so
  # that files/dirs will not be generated world readable per default
  umask 0027

  # adjust the oom score (which is inherited by start-stop-daemon)
  # to ensure that others are killed first in case of low memory situations
  echo -900 >/proc/$$/oom_score_adj 2>/dev/null

  # start ReGaHss (normal or in debug mode)
  if [[ "${REGA_BIN_VERSION}" == "DEBUG" ]]; then
    start-stop-daemon -S -q -b -m -p ${PIDFILE} --exec /usr/bin/script -- -q -c "/bin/${REGA_BIN_FILE} -f /etc/rega.conf -l 0 -c 2>&1" /usr/local/tmp/${REGA_BIN_FILE}-$$.log
    echo -n "DEBUG (/usr/local/tmp/${REGA_BIN_FILE}-$$.log), "
  else
    start-stop-daemon -S -q -b -m -p ${PIDFILE} --exec /bin/${REGA_BIN_FILE} -- -f /etc/rega.conf -l ${LOGLEVEL_REGA}
  fi

  # wait until ReGaHss is started.
  waitStartupComplete

  # make sure the PID file has world readable permissions
  chmod 0644 ${PIDFILE}
}

stop() {
  echo -n "Stopping ReGaHss: "

  if [[ -f ${PIDFILE} ]]; then

    # as ReGaHss is not saving its config upon termination we force a final
    # config save now
    echo -n "."
    echo "load tclrega.so; rega system.Save()" | /bin/tclsh 2>/dev/null
    sync

    # signal ReGaHss to stop
    start-stop-daemon -K -q -p ${PIDFILE}

    # wait for ReGaHss to terminate
    waitTerminationComplete
    rm -f ${PIDFILE}
  else
    echo "OK"
  fi
}

restart() {
  stop
  start
}

case "$1" in
  start)
    start
  ;;
  stop)
    stop
  ;;
  restart|reload)
    restart
  ;;
  *)
    echo "Usage: $0 {start|stop|restart}"
    exit 1
esac

exit 0
